#include "../../include/PageLib/DirScanner.h"
#include <iostream>
#include <map>
#include <dirent.h>
#include <cstring>
#include <string>
using std::cout;
using std::endl;
using std::map;

DirScanner::DirScanner(Configuration *pconf)
:_conf(pconf)
{
    if(pconf)
    {
        storeFilePath();
        cout << "DirScanner()" << endl;
    }
    else
    {
        //日志
        perror("DirScanner:pconf is nullptr");
        exit(1);
    }
}

DirScanner::~DirScanner()
{
    cout << "~DirScanner" << endl;
}

void DirScanner::storeFilePath()
{
    map<string, string> confmap = _conf->getConfMap();
    DIR *pdir = opendir(confmap["DirPath"].c_str());
    if(!pdir)
    {
        //日志
        cout << "DirScanner: open"<<confmap["DirPath"]<<"fail !" << endl;
        exit(1);
    }
    struct dirent *entry;

    while ((entry=readdir(pdir))!=nullptr)
    {
        // 仅仅处理xml文件,strstr是求字符串出现位置
        if(strcmp(entry->d_name,".")!=0&&strcmp(entry->d_name,"..")!=0)
        {
            _filesPath.push_back(confmap["DirPath"] + "/" + entry->d_name);
        }
    }
    closedir(pdir);
}

vector<string>& DirScanner::getFilePath()
{
    return _filesPath;
}